<?php
/**
 *  學生資料
 * @author cik
 *
 */
class Default_Model_Upgrade_StudBase extends Default_Model_Upgrade_Core
{

    protected $_table = 'stud_base';
    protected $_targetTable = array('student_character');

    function upgrade (){
        if ($this->isUpgrade())
        return 'isUpgrde';
        else {
            // 檢查是否已升級
            $query = "SELECT COUNT(*)  AS cc FROM student";
            $res = $this->_newdbPdo->query($query)->fetch();
            $hasData = $res['cc'] ;
            $query = "SELECT * FROM stud_base ";
            $res = $this->_sfs3Pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
            foreach ($res as $row) {
                $arr = array();
                $arr['sn'] = $row['student_sn'];
                if ($hasData) {
                    $find = $this->_newdbPdo->query("SELECT sn FROM student
				  WHERE sn ='{$arr['sn']}'
				  ");
                    if ($find->rowCount())
                    continue;
                }
                $arr['student_id'] = $row['stud_id'];
                $arr['name'] = $this->iconv($row['stud_name']);
                $arr['english_name'] = $this->iconv($row['stud_name_eng']);
                $arr['sex'] = ($row['stud_sex']=='1')?'M':'F';
                $arr['birthday'] = $row['stud_birthday'];
                $arr['kind'] = $row['stud_kind'];
                $arr['nationality'] = $this->iconv($row['stud_country']);
                $arr['identification'] = $row['stud_country_kind'];
                $arr['identification_number'] = $row['stud_person_id'];
                $arr['colony'] = $this->iconv($row['stud_country_name']);
                $arr['registered_county'] = $this->iconv($row['stud_addr_a']);
                $arr['registered_town'] = $this->iconv($row['stud_addr_b']);
                $arr['registered_village'] = $this->iconv($row['stud_addr_c']);
                $arr['registered_neighbor'] = $this->iconv($row['stud_addr_d']);
                $arr['registered_road'] = $this->iconv($row['stud_addr_e']);
                $arr['registered_section'] = $this->iconv($row['stud_addr_f']);
                $arr['registered_lane'] = $this->iconv($row['stud_addr_g']);
                $arr['registered_alley'] = $this->iconv($row['stud_addr_h']);
                $arr['registered_no'] = $this->iconv($row['stud_addr_i']);
                $arr['registered_extend'] = $this->iconv($row['stud_addr_j']);
                $arr['registered_floor'] = $this->iconv($row['stud_addr_k']);
                $arr['registered_floor_extend'] = $this->iconv($row['stud_addr_l']);
                $arr['registered_other'] = $this->iconv($row['stud_addr_m']);
                $arr['registered_address'] =  $this->iconv($row['stud_addr_1']);
                $arr['address'] =  $this->iconv($row['stud_addr_2']);
                $arr['address_zip'] = $this->iconv($row['addr_zip']);
                $arr['phone_registered'] = $this->iconv($row['stud_tel_1']);
                $arr['phone_home'] = $this->iconv($row['stud_tel_2']);
                $arr['phone_mobile'] = $this->iconv($row['stud_tel_3']);
                $arr['class_property'] = $row['stud_class_kind'];
                $arr['special_kind'] = $row['stud_spe_kind'];
                $arr['special_class_kind'] = $row['stud_spe_class_kind'];
                $arr['special_class_property'] = $row['stud_spe_class_id'];
                $arr['preschool_qualification'] = $row['stud_preschool_status'];
                $arr['preschool_educational_id'] = $row['stud_preschool_id'];
                $arr['preschool_name'] = $this->iconv($row['stud_preschool_name']);
                $arr['elementary_school_qualification'] = $row['stud_mschool_status'];
                $arr['elementary_school_educational_id'] = $row['stud_mschool_id'];
                $arr['elementary_school_name'] = $this->iconv($row['stud_mschool_name']);
                $arr['enroll_year'] = $row['stud_study_year'];
                $arr['born_place'] = $row['stud_birth_place'];
                $arr['enroll_school'] =  $this->iconv($row['enroll_school']);
                $arr['address_move_in'] = $row['temp_addr_move_in'];
                $arr['blood'] = $row['stud_blood_type'];
                $arr['study_condition'] = $row['stud_study_cond'];
 $query  = "INSERT INTO  student  (".implode(',',array_keys($arr)).") VALUES('".implode("','",array_values($arr))."')";
                         $this->_newdbPdo->exec($query);

                $kind_arr = explode(",",$row['stud_kind']) ;
                foreach ($kind_arr as $val) {
                    if ($val !=='') {
                        $arr2 = array();
                        $arr2['student_sn'] =  $row['student_sn'];
                        $arr2['kind'] = $val;
                        $query  = "INSERT INTO  student_character  (".implode(',',array_keys($arr2)).") VALUES('".implode("','",array_values($arr2))."')";
                         $this->_newdbPdo->exec($query);
                    }
                }

            }
            $log['memo'] = '學生資料';
            $log['upgrade_time']	= date("Y-m-d h:i:s");

            $this->finishUpgrade($log);



        }
    }
}